$(document).ready(function(){
	$("#container").wemark360("https://matterport.oss-cn-shenzhen.aliyuncs.com/misc/hongqi/pc/image/L5/hongqi",{
        fullScreen:false
    });
    var e,i;
    e=$("#container").width();
    i=e + 'px'//500+"px";
    console.log(i,e)
    // $("#container").height(i);
    // $("#iframe").height(i);
    $("#exterior").click(function(){
        // $("#iframe").addClass("s");
        $("#container").fadeIn(1e3)
    })
});
window.imgUrl = localStorage.getItem('imgUrl') || 'huang/';
(function(e){
    var i=new Array;
    function t(i,t,n){
        var s={
            initialIndex:0,interior:"image/interior.jpg",interiorFunc:undefined,fullScreen:false,loadedFunc:undefined
        };
        this.element=i;
        this.resource=t;
        this.params=e.extend({},s,n);
        this.lowImages=[];
        this.highImages=[];
        this.startIndex=this.params.initialIndex;
        this.currentIndex=this.startIndex
    }
    t.prototype.checkImageLoad=function(e){
        if(!e){
            e="low"
        }
        var i=this.highImages;
        if(e=="low"){
            i=this.lowImages
        }
        for(var t=0;t<i.length;t++){
            var n=i[t];
            if(!n[0].loaded){
                return false
            }
        }
        return true
    };
    t.prototype.setImage=function(i){
        e(this.imageContainer).find("div[index="+this.currentIndex+"]")
    };
    t.prototype.loadHighImage=function(i){
        var t;
        for(var n=0;n<this.highImages.length;n++){
            var s=this.highImages[n];
            if(s.index==i){
                t=s;break
            }
        }
        if(!t){
            console.log(window.imgUrl)
            var r=this.resource+"_high_"+i+".jpg";
            console.log(r)
            var a=e("<img>");
            a.src= r;
            a.type="high";
            a.index=i;
            this.highImages.push(a);
            a.attr("src",r);
            a.addClass("high");
            a[0].index=i;
            e(this.imageContainer).find("div[index="+i+"]").append(a);
            a[0].onload=function(e){
                e.target.loaded=true;
                if(!this.down){
                    this.showHighImage()
                }
            }.bind(this)
        }
    };
    t.prototype.showHighImage=function(){
        for(var i=0;i<this.highImages.length;i++){
            var t=this.highImages[i];
            if(t[0].loaded){
                var n=e(this.imageContainer).find("div[index="+t.index+"]");
                n.addClass("high-image")
            }
        }
    };
    t.prototype.findImage=function(e){
        for(var i=0;i<this.highImages.length;i++){
            var t=this.highImages[i];
            if(t.index==e&&t.loaded){
                return t.src
            }
        }
        return this.lowImages[e/10].src};
    t.prototype.preload=function(){
        for(var i=0;i<360;i+=10){
            var t=e("<div>").addClass("image-wrapper").attr("index",i);
            var n=e(this.element).width();
            var s=e(this.element).height();
            if(this.params.fullScreen){
                n=window.innerWidth;
                s=window.innerHeight
            }
            
            
            
            var r= "img/"+window.imgUrl+(360-i)+".jpg";
            
            
            
            // console.log(r);
            
            var a=e("<img>");
            t.append(a);a.src=r;
            a.index=i;
            a.type="low";
            this.lowImages.push(a);
            a.attr("src",r);a.addClass("low");
            if(!this.params.fullScreen){
                if(n/s>1e3/259){
                    e(this.element).addClass("wemark360-by-width")
                }else{
                    e(this.element).addClass("wemark360-by-height")
                }
            }else{
                if(n/s>1920/1080){
                    e(this.element).addClass("wemark360-by-width-fullscreen")
                }else{e(this.element).addClass("wemark360-by-height-fullscreen")
                }
            }e(this.imageContainer).append(t);
            if(!this.params.fullScreen){
                a[0].onload=function(e){
                    e.target.loaded=true;
                    if(this.checkImageLoad()){
                        if(this.params.loadedFunc){
                            this.params.loadedFunc()
                        }this.loaded=true}
                }.bind(this)
            }
        }
        this.showImage(this.currentIndex)
    };
    t.prototype.showImage=function(i){
        e(this.imageContainer).find("div").removeClass("image-container-show");
        e(this.imageContainer).find("div[index="+i+"]").addClass("image-container-show");
        if(this.params.fullScreen){
            this.loadHighImage(i)}
    };
    t.prototype.leftRotate=function(){
        this.currentIndex=this.currentIndex-10+360*10;
        this.currentIndex%=360;this.showImage(this.currentIndex)
    };
    t.prototype.rightRotate=function(){
        this.currentIndex=this.currentIndex+10+360*10;
        this.currentIndex%=360;
        this.showImage(this.currentIndex)
    };
    t.prototype.fullScreen=function(){
        if(this.params.fullScreen){
            e(this.element).remove()
        }else{var i=e.extend({},this.params,{
            fullScreen:true,initialIndex:this.currentIndex
        });
            var t=e("<div>").wemark360(this.resource,i);
            e(document.body).append(t)}
    };
    t.prototype.nativeFullScreen=function(){
        if(e(this.element)[0].webkitRequestFullscreen){
            e(this.element)[0].webkitRequestFullscreen()
        }else{this.innerFullscreen()
        }
    };t.prototype.innerFullscreen=function(){
        var i=e("<div>");var t=window.innerWidth;
        var n=window.innerHeight;
        i.css("position","fixed").css("top","0px").css("left","0px").css("width",t+"px").css("height",n+"px").css("backgroundColor","red");
        i.click(function(){document.body.removeChild(e(this)[0])});
        document.body.appendChild(i[0])};
    t.prototype.onMouseDown=function(e){
        this.down=true;this.startPositionY=e.touches?e.touches[0].clientY:e.offsetY};
    t.prototype.oriListener=function(e){
        var _this =this
        // setTimeout(function(){
                // For FF3.6+
            if (!e.gamma && !e.beta) {
                // angle=radian*180.0/PI 在firefox中x和y是弧度值,
                e.gamma = (e.x * (180 / Math.PI)); //转换成角度值,
                e.beta = (e.y * (180 / Math.PI)); //转换成角度值
                e.alpha = (e.z * (180 / Math.PI)); //转换成角度值
            }
            /* beta:  -180..180 (rotation around x axis) */
            /* gamma:  -90..90  (rotation around y axis) */
            /* alpha:    0..360 (rotation around z axis) (-180..180) */

            var gamma = e.gamma
            var beta = e.beta
            var alpha = e.alpha

            // if(e.accelerationIncludingGravity){
            //     // window.removeEventListener('deviceorientation', this.orientationListener, false);
            // }
                gamma = e.accelerationIncludingGravity.x*300
                beta = e.accelerationIncludingGravity.y
                // alpha = event.accelerationIncludingGravity.z*300
            // alert(e.accelerationIncludingGravity.x)
            if(!this.down){
                // var i=beta;
                // var t=Math.floor(beta/30);
                // _this.currentIndex=(t*10+360*10);
                // // alert(_this.currentIndex)
                // _this.currentIndex%=360;
                var i = Math.round(beta%=360)
                // alert(i)
                this.showImage(i)
            }
            
        // },10);
        };
    t.prototype.onMouseMove=function(e){
        e.preventDefault();
        if(this.down&&this.loaded){
            var i=e.touches?e.touches[0].clientY:e.offsetY;
            var t=Math.floor((i-this.startPositionY)/30);
            this.currentIndex=this.startIndex+(t*10+360*10);
            this.currentIndex%=360;
            alert(this.currentIndex)
            this.showImage(this.currentIndex)}};
    t.prototype.onMouseUp=function(e){
        this.down=false;
        this.startIndex=this.currentIndex;
        this.showHighImage();console.log("mouse up")};
    t.prototype.onMouseOut=function(e){};
    t.prototype.onDoubleClick=function(e){
        this.nativeFullScreen()};
    t.prototype.initEvent=function(){
        var _this = this
        e(this.imageContainer).on("mousedown touchstart",this.onMouseDown.bind(this));
        e(this.imageContainer).on("mousemove touchmove",this.onMouseMove.bind(this));
        e(this.imageContainer).on("mouseup touchend",this.onMouseUp.bind(this));
        e(this.imageContainer).on("dblclick",this.onDoubleClick.bind(this));
        e(this.imageContainer).on("mouseout touchcancel",this.onMouseOut.bind(this));
        window.addEventListener('devicemotion', function(e){if(!this.down){
            var i = Math.round(e.accelerationIncludingGravity.y%=360)
            i = i == Math.abs(i)?i:36+i
            _this.showImage(i*10)
        }}, false);
        e(this.element).find(".leftArrow").click(this.leftRotate.bind(this));
        e(this.element).find(".rightArrow").click(this.rightRotate.bind(this));
        e(this.element).find(".fullScreen").click(this.fullScreen.bind(this));
        e(this.element).find(".close").click(this.fullScreen.bind(this))};
    t.prototype.initUIElement=function(){
        var i=e("<div>");
        var t=e("<div>");
        var n=e("<div>");
        var s=e("<div>");
        e(t).addClass("leftArrow");
        e(n).addClass("rightArrow");
        e(s).addClass("fullScreen");
        e(i).addClass("ui-container");
        e(i).append(t);
        e(i).append(n);
        e(i).append(s);
        e(this.element).append(i)
    };
    t.prototype.init=function(){
        var i=e("<div>");e(i).addClass("image-container");
        this.imageContainer=i;
        e(this.element).append(this.imageContainer);
        e(this.imageContainer).css("cursor","url(image/cursor-left-right.cur), pointer");
        var t=e("<div>").addClass("icon-360");
        e(this.element).append(t);
        var n=e("<div>").addClass("interior").attr("title","切换内饰");
        e(this.element).append(n);
        n.css("background-image","url("+this.params.interior+")");
        e(n).click(function(){
            e("#iframe").removeClass("s");
            var i=e("#iframe1").attr("src");
            e("#iframe_container2").addClass("yin");
            if(typeof i=="undefined"){
                e("#iframe1").attr("src","https://www.automark.cc/show?m=L5neishi");
                e("#load-icon").css("display","block");
                e("#iframe1").on("load",function(){e("#load-icon").css("display","none");
                    e("#container").fadeOut(3e3)
                })
            }else{
                e("#container").fadeOut(1500)
            }
        }.bind(this));
        e(this.element).addClass("wemark360");
        if(this.params.fullScreen){
            e(this.element).addClass("fullscreen-container");
            var s=e("<div>").addClass("full-screen-header");
            e(this.element).append(s);
            var r=e("<div>").addClass("close");
            e(s).append(r)
        }if(this.params.fullScreen){
            this.loaded=true
        }this.initUIElement();
        this.initEvent();
        this.preload()};
    window.Wemark360=t;
    e.fn.extend({
        wemark360:function(i,n){
            var s=new t(this,i,n);
            s.init();
            return e(this)
        }
    })
})(jQuery);